package com.cjd.view.radar

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import com.cjd.common.R
import java.util.*
import kotlin.math.min

/**
 * @Author chenjidong
 * @email 374122600@qq.com
 * created 2019/9/20
 * description 雷达扫描
 */
class RadarScanView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attributeSet, defStyleAttr) {

    private val DEFAULT_WIDTH = 250f
    val DEFAULT_HEIGHT = 250f

    private var defaultWidth: Int = 0
    private var defaultHeight: Int = 0
    private var degrees: Float = 0F
    private var centerX: Int = 0
    private var centerY: Int = 0
    private var radarRadius: Int = 0
    public var circleColor = Color.parseColor("#07ba62")
    public var radarColor = Color.parseColor("#9907ba62")
    public var tailColor: Int = Color.parseColor("#07ba62")
    private var interval = 2
    private var shader: SweepGradient? = null

    fun setInterval(interval: Int) {
        this.interval = interval
    }

    private val mPaintCircle: Paint by lazy {
        Paint().apply {
            this.color = circleColor
            this.isAntiAlias = true//抗锯齿
            this.style = Paint.Style.STROKE//设置实心
            this.strokeWidth = 2f//画笔宽度
        }
    }

    private val mPaintRadar: Paint by lazy {
        Paint().apply {
            this.color = radarColor
            this.isAntiAlias = true
        }
    }
    private var mMatrix: Matrix? = null

    private val timer: Timer by lazy {
        Timer()
    }

    init {
        mMatrix = Matrix()


        if (attributeSet != null) {
            val ta = context.obtainStyledAttributes(
                attributeSet,
                R.styleable.RadarScanView
            )
            circleColor = ta.getColor(R.styleable.RadarScanView_radar_circle_color, circleColor)
            radarColor = ta.getColor(R.styleable.RadarScanView_radar_color, radarColor)
            tailColor = ta.getColor(R.styleable.RadarScanView_radar_tail_color, tailColor)
            ta.recycle()
        }

        defaultWidth = dip2px(context, DEFAULT_WIDTH)
        defaultHeight = dip2px(context, DEFAULT_HEIGHT)

        timer.schedule(object : TimerTask() {
            override fun run() {
                degrees += interval

                mMatrix = Matrix()
                mMatrix?.postRotate(degrees, centerX.toFloat(), centerY.toFloat())
                postInvalidate()
                if (degrees % 360F == 0F)
                    degrees = 0F
            }

        }, 10L, 10L)
    }


    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        timer?.cancel()
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        centerX = w / 2
        centerY = h / 2
        radarRadius = min(w, h)
        shader = SweepGradient(centerX.toFloat(), centerY.toFloat(), Color.TRANSPARENT, tailColor)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        var resultWidth = 0
        val modeWidth = MeasureSpec.getMode(widthMeasureSpec)
        val sizeWidth = MeasureSpec.getSize(widthMeasureSpec)

        if (modeWidth == MeasureSpec.EXACTLY) {
            resultWidth = sizeWidth
        } else {
            resultWidth = defaultWidth
            if (modeWidth == MeasureSpec.AT_MOST) {
                resultWidth = min(resultWidth, sizeWidth)
            }
        }

        var resultHeight = 0
        val modeHeight = MeasureSpec.getMode(heightMeasureSpec)
        val sizeHeight = MeasureSpec.getSize(heightMeasureSpec)
        if (modeHeight == MeasureSpec.EXACTLY) {
            resultHeight = sizeHeight
        } else {
            resultHeight = defaultHeight
            if (modeHeight == MeasureSpec.AT_MOST) {
                resultHeight = min(resultHeight, sizeHeight)
            }
        }

        setMeasuredDimension(resultWidth, resultHeight)
    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        //分别绘制四个圆
        canvas.drawCircle(
            centerX.toFloat(),
            centerY.toFloat(),
            (radarRadius / 7).toFloat(),
            mPaintCircle
        )

        canvas.drawCircle(
            centerX.toFloat(),
            centerY.toFloat(),
            (radarRadius / 4).toFloat(),
            mPaintCircle
        )

        canvas.drawCircle(
            centerX.toFloat(),
            centerY.toFloat(),
            (radarRadius / 3).toFloat(),
            mPaintCircle
        )
        canvas.drawCircle(
            centerX.toFloat(),
            centerY.toFloat(),
            (3 * radarRadius / 7).toFloat(),
            mPaintCircle
        )

        //设置颜色渐变从透明到不透明
        //       Shader shader = new SweepGradient(centerX, centerY, Color.parseColor("#0052a8f7"), Color.parseColor("#ff52a8f7"));
        mPaintRadar.shader = shader
        canvas.concat(mMatrix)
        canvas.drawCircle(
            centerX.toFloat(),
            centerY.toFloat(),
            (3 * radarRadius / 7).toFloat(),
            mPaintRadar
        )
    }

    private fun dip2px(context: Context, dipValue: Float): Int {
        val scale = context.resources.displayMetrics.density
        return (dipValue * scale + 0.5f).toInt()
    }
}